﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Sinacor.Infra.Core.Licensing.Business;
using WebLicenseGenerator.UC;

namespace WebLicenseGenerator
{
	public partial class ManageAliases : System.Web.UI.Page
	{
		Alias _aliasBusiness = null;
		int _selectedAliasCode
		{
			get
			{
				if (ViewState["SelectedAliasCode"] != null)
					return Convert.ToInt32(ViewState["SelectedAliasCode"]);
				else
					return 0;
			}
			set { ViewState["SelectedAliasCode"] = value; }
		}
		List<Sinacor.Infra.Core.Licensing.Alias> _aliasTable
		{
			get { return ViewState["AliasTable"] as List<Sinacor.Infra.Core.Licensing.Alias>; }
			set { ViewState["AliasTable"] = value; }
		}
		AliasInput _inputModal;
		Confirmation _deleteConfirmation;

		void LoadAliases()
		{
			_aliasBusiness = new Alias();

			_aliasTable = _aliasBusiness.Selecionar(new Sinacor.Infra.Core.Licensing.Alias());
			grdAlias.DataSource = _aliasTable;
			grdAlias.DataBind();
		}

		void ClearGridView()
		{
			grdAlias.DataSource = null;
			this.BindGridView();
		}

		void BindGridView()
		{
			grdAlias.DataBind();
		}

		void grdAlias_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			_selectedAliasCode = 0;
			int aliasId = Convert.ToInt32(e.CommandArgument);
			Sinacor.Infra.Core.Licensing.Alias selectedAlias = null;

			if (e.CommandName == "UpdateSelected")
			{
				foreach (Sinacor.Infra.Core.Licensing.Alias alias in _aliasTable)
				{
					if (alias.ID == aliasId)
					{
						selectedAlias = alias;
						break;
					}
				}

				if (selectedAlias != null)
					ShowCompanyInputDialog("Alteração de Alias", "Altere os dados desejados", selectedAlias);
			}
			else if (e.CommandName == "DeleteSelected")
			{
				foreach (Sinacor.Infra.Core.Licensing.Alias alias in _aliasTable)
				{
					if (alias.ID == aliasId)
					{
						selectedAlias = alias;
						_selectedAliasCode = alias.ID.Value;
						break;
					}
				}

				if (selectedAlias != null)
					ShowConfirmationDialog("Confirmação de Exclusão", "Tem certeza que deseja deletar o Alias selecionado?");
			}
		}

		void grdAlias_RowUpdating(object sender, GridViewUpdateEventArgs e)
		{
		}

		void _inputModal_OnAction(object sender, AliasInputEventArgs e)
		{
			if (e.Result == AliasInputResult.Save)
				this.RefreshGridView();
		}

		void RefreshGridView()
		{
			_aliasBusiness = new Alias();
			List<Sinacor.Infra.Core.Licensing.Alias> aliases = _aliasBusiness.Selecionar(new Sinacor.Infra.Core.Licensing.Alias());

			grdAlias.DataSource = aliases;
			_aliasTable = aliases;

			this.BindGridView();
		}

		void ClearNewAliasForm()
		{
			txtNovoAlias.Text = "";
		}

		void OnConfirmation(object sender, ConfirmationEventArgs e)
		{
			if (e.Result == ConfirmationResult.Yes)
			{
				_aliasBusiness = new Alias();
				_aliasBusiness.Excluir(new Sinacor.Infra.Core.Licensing.Alias(_selectedAliasCode));

				this.ShowMessageDialog("Exclusão de Alias", "Alias excluido com sucesso.");

				this.RefreshGridView();
			}
		}

		bool ValidatePageFields()
		{
			return (txtNovoAlias.IsValid);
		}

		protected void InsertNewAlias(object sender, EventArgs e)
		{
			if (ValidatePageFields())
			{
				_aliasBusiness = new Alias();
				_aliasBusiness.Incluir(new Sinacor.Infra.Core.Licensing.Alias(txtNovoAlias.Text.Trim()));

				this.RefreshGridView();
				this.ClearNewAliasForm();
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
				this.LoadAliases();

			_inputModal = ((WebLicenseGenerator.UC.AliasInput)this.Master.FindControl("aliasInput"));
			_inputModal.OnAction += new AliasInput.AliasInputEventHandler(_inputModal_OnAction);

			grdAlias.RowCommand += new GridViewCommandEventHandler(grdAlias_RowCommand);
			grdAlias.RowUpdating += new GridViewUpdateEventHandler(grdAlias_RowUpdating);

			_deleteConfirmation = ((WebLicenseGenerator.UC.Confirmation)this.Master.FindControl("confirmationPanel"));
			_deleteConfirmation.OnConfirmation += new Confirmation.ConfirmationEventHandler(OnConfirmation);
		}

		void ShowConfirmationDialog(string title, string message)
		{
			_deleteConfirmation.Message = message;
			_deleteConfirmation.Title = title;
			_deleteConfirmation.ShowConfirmationDialog();
		}

		void ShowMessageDialog(string title, string message)
		{
			WebLicenseGenerator.UC.Messaging messaging = ((WebLicenseGenerator.UC.Messaging)this.Master.FindControl("errorMessaging"));
			messaging.Message = message;
			messaging.Title = title;
			messaging.ShowMessageDialog();
		}

		void ShowCompanyInputDialog(string title, string message, Sinacor.Infra.Core.Licensing.Alias alias)
		{
			_inputModal.Title = title;
			_inputModal.Message = message;

			_inputModal.AliasId = alias.ID.Value;
			_inputModal.AliasName = alias.Nome;

			_inputModal.ShowInputDialog();
		}
	}
}